import { createRouter, createWebHistory } from "vue-router";
import message from '@/static/js/message.js';

/**
 * 重写路由的push方法
*/

const routerPush = createRouter.prototype.push;
createRouter.prototype.push = function push(location) {
    if(typeof(location) == 'string'){
        let Separator = '&';
        if(location.indexOf('?') == -1) {
            Separator == '?';
        }
        location = location + Separator + 'random' + Math.random();
    }
    return routerPush.call(this, location).catch(error=> error)
};

//路由
const routes = [
    //登录
    {
        path: '/',
        name: '登录',
        component: () => import('@/views/Login/Login.vue'),
    },
    //错误页
    {
        path: '/404',
        name: '404错误页',
        component: () => import('@/views/SinglePage/404.vue'),
    },
    {
        path: '/403',
        name: '403权限页',
        component: () => import('@/views/SinglePage/403.vue'),
    },
    //首页
    {
        path: '/',
        component: () => import('@/components/navMain/navMain.vue'),
        children: [
            {
                path: '/Index',
                name: '首页',
                component: () => import('@/views/Index/Index.vue'),
                meta:{
                    requireAuth: true
                }
            },
        ]
    },
    //项目管理
    {
        path: '/',
        component: () => import('@/components/navMain/navMain.vue'),
        children: [
            {
                path: '/ProjectList',
                name: '项目列表',
                component: () => import('@/views/ProjectManage/ProjectList.vue'),
                meta:{
                    requireAuth: true
                }
            },
            {
                path: '/DataList',
                name: '数据管理',
                component: () => import('@/views/ProjectManage/DataList.vue'),
                meta:{
                    requireAuth: true
                }
            },
        ]
    },
    //系统管理
    {
        path: '/',
        component: () => import('@/components/navMain/navMain.vue'),
        children: [
            {
                path: '/RoleManage',
                name: '权限管理',
                component: () => import('@/views/SystemManage/RoleManage.vue'),
                meta:{
                    requireAuth: true
                }
            },
            {
                path: '/AccountManage',
                name: '账号管理',
                component: () => import('@/views/SystemManage/AccountManage.vue'),
                meta:{
                    requireAuth: true
                }
            },
            {
                path: '/DataManage',
                name: '数据字典',
                component: () => import('@/views/SystemManage/DataManage.vue'),
                meta:{
                    requireAuth: true
                }
            },
            {
                path: '/MessageManage',
                name: '消息中心',
                component: () => import('@/views/SystemManage/MessageManage.vue'),
                meta:{
                    requireAuth: true
                }
            },
            {
                path: '/LogsManage',
                name: '登录日志',
                component: () => import('@/views/SystemManage/LogsManage.vue'),
                meta:{
                    requireAuth: true
                }
            },
        ], 
    },
];

//创建路由
const router = createRouter({
    history: createWebHistory(process.env.BASE_URL),
    routes,
});

// 全局守卫
router.beforeEach((to, from, next) => {
    let menuData = JSON.parse(localStorage.getItem('menuData'));
    let authKey = localStorage.getItem("authKey");
    //判断访问登录页存在token时,跳转/Index
    if(to.path == '/' && authKey){
        next({
            path: '/Index'
        });
    }
    
    //判断路由是否需要权限
    if(to.matched.some(record => record.meta.requireAuth)){
        //判断用户路由数据是否有权限
        let array = [];
        if(to.path != '/Index'){
            //重新遍历用户路由地址
            for (let index = 0; index < menuData.length; index++) {
                if(menuData[index].children){
                    for (let key = 0; key < menuData[index].children.length; key++) {
                        array.push(menuData[index].children[key].node_path)
                    }
                }
            }
            //判断用户路由是否与
            if(array.includes(to.path)){
                //有权限
            }else{
                //无权限
                next({
                    path: '/403'
                })
            }
        }
        //判断token是否存在
        if(!authKey){
            next({
                path: '/',
                query: { redirect: to.fullPath }
            })
        }
    }
    
    //404跳转
    if(to.matched.length === 0){
        next({path: '/404'})
    }else{
        next();
    }
})

export default router;